package com.pounds.service.impl;

import com.pounds.mapper.AdminMapper;
import com.pounds.pojo.Admin;
import com.pounds.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    enum AdminEnumError {
        Normal,
        DataFormatError,
        EmptyError,
        DataNotFoundError,
        DataError
    }

    @Override
    public List<Admin> getAdminList() {
        return adminMapper.getAdminList();
    }

    @Override
    public String addAdmin(String name, String account, String password, String contact, String note) {
        if (name == null || account == null || password == null || contact == null) {
            return AdminEnumError.DataFormatError.toString();
        } else if (name.isEmpty() || account.isEmpty() || password.isEmpty() || contact.isEmpty()) {
            return AdminEnumError.EmptyError.toString();
        }
        adminMapper.addAdmin(name, account, password, contact, note);
        return UserServiceImpl.UserEnumError.Normal.toString();
    }

    @Override
    public String deleteAdmin(int id) {
        List<Admin> admin = adminMapper.selectAdmin(id);
        if (admin == null) {
            return AdminEnumError.DataNotFoundError.toString();
        }
        adminMapper.deleteAdmin(id);
        return AdminEnumError.Normal.toString();
    }

    @Override
    public String updateAdmin(int id, String name, String account, String password, String contact, String note) {
        if (name == null || account == null || password == null || contact == null || id < 0) {
            return AdminEnumError.DataFormatError.toString();
        } else if (name.isEmpty() || account.isEmpty() || password.isEmpty() || contact.isEmpty()) {
            return AdminEnumError.EmptyError.toString();
        }
        adminMapper.selectAdmin(id);
        List<Admin> admin = adminMapper.selectAdmin(id);
        if (admin == null) {
            return AdminEnumError.DataNotFoundError.toString();
        }
        adminMapper.updateAdmin(id, name, account, password, contact, note);
        return AdminEnumError.Normal.toString();
    }

    @Override
    public String login(String account, String password) {
        if (account == null || password == null) {
            return AdminEnumError.DataFormatError.toString();
        } else if (account.isEmpty() || password.isEmpty()) {
            return AdminEnumError.EmptyError.toString();
        }
        String adminPassword = adminMapper.findAdminPassword(account, password);
        if (adminPassword == null) {
            return AdminEnumError.DataNotFoundError.toString();
        } else if (adminPassword.isEmpty()) {
            return AdminEnumError.DataNotFoundError.toString();
        } else if (adminPassword.equals(password)) {
            return AdminEnumError.Normal.toString();
        } else {
            return AdminEnumError.DataError.toString();
        }
    }
}
